(function(window) {
  let isIOS = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);

  function getIOSBridge(callback) {
    return new Promise(resolve => {
      if (window.WebViewJavascriptBridge) {
        return resolve(WebViewJavascriptBridge);
      }
      if (window.WVJBCallbacks) {
        return window.WVJBCallbacks.push(resolve);
      }
      window.WVJBCallbacks = [resolve];
      var WVJBIframe = document.createElement("iframe");
      WVJBIframe.style.display = "none";
      WVJBIframe.src = "https://__bridge_loaded__";
      document.documentElement.appendChild(WVJBIframe);
      setTimeout(function() {
        document.documentElement.removeChild(WVJBIframe);
      }, 0);
    }).then(callback);
  }

  async function getBridge(callback) {
    await getIOSBridge(callback);
  }

  function loadJSBridge(main) {
    class JSBridge {
      constructor() {
        const self = this;
        return function JSBFn(opts, cb) {
          if (self[this]) {
            self[this](opts, cb);
          }
        };
      }

      // 获取当前定位
      getCurrentLocation(opts, cb) {
        if (isIOS) {
          WebViewJavascriptBridge.callHandler(
            "getCurrentLocation",
            "",
            function(response) {
              cb(response);
            }
          );
        } else {
          try {
            const data = JSON.parse(window.mobile.getCurrentLocation());
            cb(data);
          } catch (e) {
            console.error(e);
            cb({ error: 1 });
          }
        }
      }

      // 获取设备号
      getDeviceId(cb) {
        if (isIOS) {
          WebViewJavascriptBridge.callHandler("getDeviceId", "", function(
            response
          ) {
            cb(response);
          });
        } else {
          const data = window.mobile.getDeviceId();
          cb(data);
        }
      }

      // 设置导航栏标题
      setTitle(opts) {
        if (isIOS) {
          WebViewJavascriptBridge.callHandler("setTitle", opts);
        } else {
          window.mobile.setTitle(JSON.stringify(opts));
        }
      }

      // 关闭当前 webview
      popWindow() {
        if (isIOS) {
          WebViewJavascriptBridge.callHandler("popWindow");
        } else {
          window.mobile.popWindow();
        }
      }
      // 打开用车订单列表
      clickCarOrder() {
        if (isIOS) {
          WebViewJavascriptBridge.callHandler("clickCarOrder");
        } else {
          window.mobile.clickCarOrder();
        }
      }

      // 打开用车订单详情
      pushCarOrderDetail(opts) {
        if (isIOS) {
          WebViewJavascriptBridge.callHandler("pushCarOrderDetail", opts);
        } else {
          window.mobile.pushCarOrderDetail(JSON.stringify(opts));
        }
      }

      // 用车打开支付
      goCarPay(opts) {
        if (isIOS) {
          WebViewJavascriptBridge.callHandler("goCarPay", opts);
        } else {
          window.mobile.goCarPay(JSON.stringify(opts));
        }
      }

      // 打开新的 webview
      pushWindow(opts) {
        if (isIOS) {
          WebViewJavascriptBridge.callHandler("pushWindow", opts);
        } else {
          window.mobile.pushWindow(JSON.stringify(opts));
        }
      }

      // 获取系统信息
      getSystemInfo(cb) {
        if (isIOS) {
          WebViewJavascriptBridge.callHandler("getSystemInfo", "", function(
            response
          ) {
            cb(response);
          });
        } else {
          const data = JSON.parse(window.mobile.getSystemInfo());
          cb(data);
        }
      }

      // 调起⽀付收银台
      tradePay(opts, cb) {
        if (isIOS) {
          WebViewJavascriptBridge.callHandler("tradePay", opts, function(
            response
          ) {
            cb(response);
          });
        } else {
          const data = JSON.parse(window.mobile.tradePay(JSON.stringify(opts)));
          cb(data);
        }
      }

      getAuthData(cb) {
        if (isIOS) {
          WebViewJavascriptBridge.callHandler("getAuthData", "", function(
            response
          ) {
            cb(response);
          });
        } else {
          const data = JSON.parse(window.mobile.getAuthData());
          cb(data);
        }
      }
    }

    window.JSBridge = new JSBridge();
    window.JSBridgeKeys = Object.getOwnPropertyNames(JSBridge.prototype);
    if (isIOS) {
      getBridge(main);
    } else {
      main();
    }
  }

  window.loadJSBridge = loadJSBridge;
  loadJSBridge(() => {
    console.log("loadJSBridge success");
  });
})(window);
